package com.example.demoweb.util;

import java.io.Serializable;

public class Data<T>
  implements Serializable
{
  private static final long serialVersionUID = 1L;
  /*
   * 0：成功，1：失败/报错
   */
  private String result;
  /*
   * 出错后的提示信息
   */
  private String message;
  /*
   * 错误编码
   */
  private String msgcode;
  /*
   * 业务数据
   */
  private T resultData;
  

  public boolean isSuccess()
  {
    return this.result == "0"? true : false;
  }

  public String getResult() {
	return result;
  }

  public void setResult(String result) {
	this.result = result;
  }

  public String getMessage() {
	return message;
  }

  public void setMessage(String message) {
	this.message = message;
  }

  public String getMsgcode() {
	return msgcode;
  }

  public void setMsgcode(String msgcode) {
	this.msgcode = msgcode;
  }

  public T getResultData() {
	return resultData;
  }

  public void setResultData(T resultData) {
	this.resultData = resultData;
  }

  public static <T> Data<T> ok(T data)
  {
    Data result = new Data();
    result.setResultData(data);
    result.setResult("0");
    return result;
  }

  public static <T> Data<T> ok(T data, String message)
  {
    Data result = new Data();
    result.setResultData(data);
    result.setResult("0");
    result.setMessage(message);
    return result;
  }

  public static <T> Data<T> ok()
  {
    return ok(null);
  }

  public static <T> Data<T> fail(T data)
  {
    Data result = new Data();
    result.setResultData(data);
    result.setResult("1");
    return result;
  }

  public static <T> Data<T> fail()
  {
    return fail(null);
  }

  public static <T> Data<T> fail(String message, T data)
  {
    Data result = new Data();
    result.setMessage(message);
    result.setResultData(data);
    result.setResult("1");
    return result;
  }

  public static <T> Data<T> fail(String errorCode, String message, T data)
  {
    Data result = new Data();
    result.setMsgcode(errorCode);
    result.setMessage(message);
    result.setResultData(data);
    result.setResult("1");
    return result;
  }
}